// pages/sell/sell.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 导航栏 高度
    navHeight: app.globalData.navHeight,
    logoBgColor: 'transparent',
    scrollTop: 0,
    soursedata:{"Honda":["Civic","Accord","Fit","CR-V","HR-V","Pilot","Ridgeline"],"Toyota":["Corolla","Camry","RAV4","Highlander","Tacoma","Sienna","Tundra"],"Ford":["F-150","Escape","Explorer","Mustang","Edge","Ranger"],"Chevrolet":["Silverado 1500","Equinox","Malibu","Traverse","Colorado","Tahoe"],"Tesla":["Model 3","Model Y","Model S","Model X"],"BMW":["3 Series","5 Series","X3","X5","X7"],"Mercedes-Benz":["C-Class","E-Class","GLC","GLE","S-Class"],"Nissan":["Altima","Sentra","Rogue","Pathfinder","Murano"],"Hyundai":["Elantra","Sonata","Tucson","Santa Fe","Kona"],"Kia":["Soul","Seltos","Sportage","Sorento","Telluride"],"Mazda":["Mazda3","Mazda6","CX-3","CX-5","CX-9"],"Volkswagen":["Golf","Jetta","Tiguan","Passat","Atlas"],"Subaru":["Outback","Forester","Impreza","Ascent","Crosstrek"],"Audi":["A3","A4","A6","Q5","Q7"],"Jeep":["Wrangler","Cherokee","Grand Cherokee","Compass","Renegade"]},
    // 品牌
    brandArray: [
        { id: 1, name: 'Honda' },
        { id: 2, name: 'Toyota' },
        { id: 3, name: 'Ford' },
        { id: 4, name: 'Chevrolet' },
        { id: 5, name: 'Tesla' },
        { id: 6, name: 'BMW' },
        { id: 7, name: 'Mercedes-Benz' },
        { id: 8, name: 'Nissan' },
        { id: 9, name: 'Hyundai' },
        { id: 10, name: 'Kia' },
        { id: 11, name: 'Mazda' },
        { id: 12, name: 'Volkswagen' },
        { id: 13, name: 'Subaru' },
        { id: 14, name: 'Audi' },
        { id: 15, name: 'Jeep' },
    ],
    brandIndex: null,
    brandItem: '',

    // 车型
    modelArray: [
      //{ id: 1, name: '宝马x3' },
      //{ id: 2, name: '宝马x5' },
      //{ id: 3, name: '宝马x7' }
    ],
    modelIndex: null,
    modelItem: '',

    // 年款
    yearArray: [
     // { id: 1, name: '2022款' },
     // { id: 2, name: '2023款' },
     // { id: 3, name: '2024款' }
    ],
    yearIndex: null,
    yearItem: '',

    form: {
      tel_mail: '',
      contact: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
    onLoad(options) {
        let arr = []
        let inum = 1
        for (let i = 2025; i >= 1970; i--){
            arr.push({ id: inum, name: i + '款' })
            inum++
        }
        this.setData({
            yearArray:arr
        })
  },

  brandChange: function(e) {
    let index = e.detail.value
    this.setData({
      brandIndex: index,
      brandItem: this.data.brandArray[index].name
    })
      let bname = this.data.brandItem;
      let modelarr = this.data.soursedata[bname];
      let i = 1;
      let modelshow = modelarr.map((item) => {
          i++
          return {'id':i,'name':item}
      })
      this.setData({
          modelArray: modelshow,
          modelIndex: null,
          modelItem: '',
      })
  },
  modelChange: function(e) {
    let index = e.detail.value
    this.setData({
      modelIndex: index,
      modelItem: this.data.modelArray[index].name
    })
  },
  yearChange: function(e) {
    let index = e.detail.value
    this.setData({
      yearIndex: e.detail.value,
      yearItem: this.data.yearArray[index].name
    })
  },

  handleCarModel: function(e) {
    this.setData({
      'form.car_model': e.detail.value
    })
  },
  handleTelMail: function(e) {
    this.setData({
      'form.tel_mail': e.detail.value
    })
  },
  handleContact: function(e) {
    this.setData({
      'form.contact': e.detail.value
    })
  },

  // 页面滚动，改变顶部logo背景
  onPageScroll: function (e) {
    let scrollTop = e.scrollTop;
    // 最大透明度为1，滚动距离超过400px后不再增加透明度
    let opacity = Math.min(1, scrollTop / 400);
    if (scrollTop === 0) {
        this.setData({
            logoBgColor: 'transparent'
        });
    } else {
        this.setData({
            logoBgColor: `rgba(0, 0, 0, ${opacity})`
        });
    }
    this.setData({
        scrollTop: scrollTop
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },
    msgadd() {
        if (this.data.brandItem == '') {
            app.m_showhint("请选择品牌车型")
            return;
        }
        if (this.data.modelItem == '') {
            app.m_showhint("请选择车型")
            return;
        }
        if (this.data.yearItem == '') {
            app.m_showhint("请选择年份")
            return;
        }
        if (this.data.form.tel_mail == '') {
            app.m_showhint("请输入联系方式")
            return;
        }
        if (this.data.form.contact == '') {
            app.m_showhint("请输入联系人")
            return;
        }
        let car_model = this.data.brandItem + ' > ' + this.data.modelItem
        car_model += '  年份:'+this.data.yearItem
        app.post("c=msg&a=addmsg", {
            type: 2,
            contact: this.data.form.tel_mail,
            contactuname: this.data.form.contact,
            cartype: car_model,
            rid: '',
            descinfo: ''
        }).then((data) => {
            app.m_showhint("提交成功，我们的工作人员会尽快与您联系，请注意接听。")
            let form = {car_model: '',tel_mail: '',contact: ''}
          
            this.setData({
                form: form,
                brandIndex: null,
                brandItem: '',
                modelArray: [],
                modelIndex: null,
                modelItem: '',
                
                yearIndex: null,
                yearItem: '',
            })
        })
    }
    })
